﻿using System;
using System.Data;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Data;
using DBAccessLibrary;
using DBAccessLibrary.Model;
using QueryResource.Common;
using Microsoft.Win32;
using System.Collections.Generic;
using System.Windows;
using System.Threading.Tasks;
using SimpleControlLibrary.Common;
using System.Windows.Media;
using QueryResource.ViewModel;

namespace SimpleControlLibrary.UI
{
    /// <summary>
    /// GridViewControl.xaml 的交互逻辑
    /// </summary>
    [System.ComponentModel.DesignTimeVisible(false)]
    public partial class GridViewControl
    {
        public GridViewControl()
        {
            InitializeComponent();
            this.busyCtrl.CanelButton = true;
            this.busyCtrl.Cancel += new EventHandler(busyCtrl_Cancel);
            Init();
        }

        void busyCtrl_Cancel(object sender, EventArgs e)
        {
            this.ViewModel.LoadCanel();
        }

   
        #region 外界通信
      
        public Action<GridViewControl> AppendDataSucess { get; set; }
      
        /// <summary>
        /// 数据加载成功后 用于通知界面激活当前窗口
        /// </summary>
        private void WriteDataSucess()
        {
            if (AppendDataSucess != null)
            {
                AppendDataSucess(this);
            }
        }
       
        #endregion

        public GridViewModel ViewModel { get; set; }
        public void DataBind(string sql)
        {
            ViewModel.LoadData(sql);
        }
        private void Init()
        {

            ViewModel = new GridViewModel();
            ViewModel.IsLoadPage = () => false;// XmlHelper.Instance.IsPageBind;
            ViewModel.LoadMaxCount = () => 100000;  // XmlHelper.Instance.SelectMaxCount;
            ViewModel.ControlInvoke = act => this.InvokeEx(act);
            ViewModel.GridCanelEdit = GridCanelEdit;
            ViewModel.GridStartEdit = GridStartEdit;
            ViewModel.LoadDataSucess = WriteDataSucess;
            ViewModel.GridCommitEdit = GridCommitEdit;
            ViewModel.GridBind = GridControlBind;
            ViewModel.GridClear = GridControlClear;
            DataContext = this;
            this.busyCtrl.CanelButton = true;
            this.dataGrid.CanUserAddRows = false;
            this.dataGrid.CanUserDeleteRows = false;
            this.dataGrid.AutoGenerateColumns = false;
        }

        private void GridControlBind(DataTable dt)
        {
            if (dt != null)
            {
                foreach (DataColumn column in dt.Columns)
                {
                    DataGridTextColumn gc = new DataGridTextColumn { Header = column.ColumnName };
                    Binding bind = new Binding(column.ColumnName) { Mode = BindingMode.Default };
                    gc.Binding = bind;
                    if (column.ColumnName == "ROWNUMBER")
                        gc.Visibility = System.Windows.Visibility.Hidden;
                    //gc.IsReadOnly = true;
                    if (column.DataType == typeof(DateTime))
                    {
                        gc.Binding.StringFormat = "yyyy-MM-dd HH:mm:ss";
                    }
                    this.dataGrid.Columns.Add(gc);
                }
                this.dataGrid.ItemsSource = dt.DefaultView;

                this.ViewModel.ExceptToExcelCommand.RaiseCanExecuteChanged();
                this.ViewModel.ExceptToSqlCommand.RaiseCanExecuteChanged();
            }
        }
        private void GridControlClear()
        {
            this.dataGrid.Columns.Clear();
            this.dataGrid.ItemsSource = null;
        }
        private void GridCanelEdit()
        {
            this.dataGrid.CanUserAddRows = false  ;
            this.dataGrid.CanUserDeleteRows = false  ;
        }
        private void GridStartEdit()
        {
            this.dataGrid.CanUserAddRows = true;
            this.dataGrid.CanUserDeleteRows = true;
            foreach (DataGridTextColumn column in this.dataGrid.Columns)
            {
                column.IsReadOnly = false;
            }
        }
        private void GridCommitEdit()
        {
            this.dataGrid.CommitEdit();
        }
    }
}
